/*
* Copyright (C) 2012 Manuel Pérez
*
* This file is part of OpenSudoku.
*
* OpenSudoku is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenSudoku is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenSudoku. If not, see <http://www.gnu.org/licenses/>.
*
*/
package igraf.tailerra;

import igraf.ItzuliInterfazea;
import igraf.Panela;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.text.MaskFormatter;

import dBasea.Abokatua;
import dBasea.Tailerra;

public class TailerKudeaketa extends Panela {
	private static final long serialVersionUID = 1046533162431632882L;

	private JLabel telefonoa;
	private JLabel izena;
	private Abokatua abok = new Abokatua();
	private JTextField ize;
	private JTextField tele;
	private Tailerra tail = new Tailerra();

	private JButton ezeztatu;

	private JLabel Arize;

	private JTextField ariZ;

	private JLabel helbidea;

	private JTextField helb;

	private JLabel hiria;

	private JButton ok;

	private JComboBox hiri;

	public TailerKudeaketa(String tituloa,
			final ItzuliInterfazea itzuliInterfazea) {
		super(tituloa);

		JPanel erdikoPanela = this.getErdikoPanela();

		erdikoPanela.setBackground(SystemColor.activeCaption);
		erdikoPanela.setLayout(new BorderLayout());

		// Erdiko panelak
		JPanel erdikoa = new JPanel();
		erdikoa.setBackground(SystemColor.activeCaption);
		erdikoa.setLayout(new GridLayout(8, 1));

		JPanel ge = new JPanel();
		ge.setBackground(SystemColor.activeCaption);
		erdikoa.add(ge);

		JPanel arri = new JPanel();
		arri.setBackground(SystemColor.activeCaption);
		FlowLayout fl_arri = new FlowLayout();
		fl_arri.setHgap(15);
		fl_arri.setVgap(10);
		arri.setLayout(fl_arri);
		izena = new JLabel("Tailer Izena:");
		izena.setFont(new Font("Arial", Font.PLAIN, 14));
		ize = new JTextField(18);
		arri.add(izena);
		arri.add(ize);
		erdikoa.add(arri);

		JPanel arribb = new JPanel();
		arribb.setBackground(SystemColor.activeCaption);
		FlowLayout xx = new FlowLayout();
		xx.setHgap(15);
		xx.setVgap(10);
		arribb.setLayout(xx);
		Arize = new JLabel("Arduradun izena:");
		Arize.setFont(new Font("Arial", Font.PLAIN, 14));
		ariZ = new JTextField(18);
		arribb.add(Arize);
		arribb.add(ariZ);
		erdikoa.add(arribb);

		JPanel arrix = new JPanel();
		arrix.setBackground(SystemColor.activeCaption);
		FlowLayout desd = new FlowLayout();
		desd.setHgap(15);
		desd.setVgap(10);
		arrix.setLayout(desd);
		helbidea = new JLabel("Helbidea:");
		helbidea.setFont(new Font("Arial", Font.PLAIN, 14));
		helb = new JTextField(18);
		arrix.add(helbidea);
		arrix.add(helb);
		erdikoa.add(arrix);

		JPanel des = new JPanel();
		des.setBackground(SystemColor.activeCaption);
		FlowLayout flarri = new FlowLayout();
		flarri.setHgap(15);
		flarri.setVgap(10);
		des.setLayout(flarri);
		hiria = new JLabel("Hiria:");
		hiria.setFont(new Font("Arial", Font.PLAIN, 14));
		des.add(hiria);
		hiri = new JComboBox();
		try {
			boxCarga();

		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null,
					"Ezin izan da kargatu DB-tik hiri zerrenda", "Oker",
					JOptionPane.ERROR_MESSAGE);
			e.printStackTrace();
		}
		des.add(hiri);
		erdikoa.add(des);

		JPanel xy = new JPanel();
		xy.setBackground(SystemColor.activeCaption);
		FlowLayout fl = new FlowLayout();
		fl.setHgap(0);
		xy.setLayout(fl);

		JPanel arrib = new JPanel();
		arrib.setBackground(SystemColor.activeCaption);
		FlowLayout fl_carri = new FlowLayout();
		fl_carri.setHgap(15);
		fl_carri.setVgap(10);
		arrib.setLayout(fl_carri);
		telefonoa = new JLabel("Telefonoa:");
		telefonoa.setFont(new Font("Arial", Font.PLAIN, 14));
		try {
			tele = new JFormattedTextField(new MaskFormatter("#########"));
		} catch (ParseException e1) {
			e1.printStackTrace();
		}
		tele.setText("");
		tele.setColumns(10);
		arrib.add(telefonoa);
		arrib.add(tele);
		erdikoa.add(arrib);
		erdikoa.add(xy);

		JPanel behekoa = new JPanel();
		FlowLayout flowLayout = (FlowLayout) behekoa.getLayout();
		flowLayout.setHgap(110);
		flowLayout.setVgap(10);
		behekoa.setBackground(SystemColor.activeCaption);
		ok = new JButton("Tailer berria sartu");
		ok.setHorizontalAlignment(SwingConstants.LEFT);
		ok.setFont(new Font("Arial", Font.PLAIN, 14));
		behekoa.add(ok);
		ezeztatu = new JButton("Ezeztatu");
		ezeztatu.setHorizontalAlignment(SwingConstants.RIGHT);
		ezeztatu.setFont(new Font("Arial", Font.PLAIN, 14));
		behekoa.add(ezeztatu);
		erdikoPanela.add(behekoa, BorderLayout.SOUTH);

		erdikoPanela.add(erdikoa, BorderLayout.CENTER);
		erdikoPanela.add(behekoa, BorderLayout.SOUTH);

		ok.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					if (!zerbaiIdatzi()) {
						JOptionPane.showMessageDialog(null,
								"Sartu behar duzu zerbait zuriunetan\n"
										+ "Ezin da eremu hutsik egon", "Oker",
								JOptionPane.ERROR_MESSAGE);
					} else if (tail.existitzenDaTailerra(helb.getText())) {
						JOptionPane.showMessageDialog(null,
								"Tailer hau existitzen da " + (helb.getText())
										+ " helbidean \n" + "Sartu beste bat",
								"Oker", JOptionPane.ERROR_MESSAGE);
					} else {
						try {
							Object non = hiri.getSelectedItem();
							String nono = non.toString();

							int telefo = Integer.parseInt(tele.getText());
							tail.tailerBerriaSartu(ize.getText(),
									ariZ.getText(), helb.getText(), telefo,
									nono);
							JOptionPane.showMessageDialog(null,
									"Ondo sortu da tailerra", "",
									JOptionPane.INFORMATION_MESSAGE);
							eremuakGarbitu();
						} catch (NumberFormatException nfe) {
							JOptionPane.showMessageDialog(null,
									"Zenbakiak sartu telefonoko eremuan",
									"Oker", JOptionPane.ERROR_MESSAGE);
						}
					}

				} catch (Exception e) {
					JOptionPane.showMessageDialog(null,
							"Ez da aurkitu, sartu bat", "Oker",
							JOptionPane.ERROR_MESSAGE);
					e.printStackTrace();
				}
			}
		});

		ezeztatu.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				itzuliInterfazea.itzuliLehenPanela();
			}
		});
	}

	private boolean zerbaiIdatzi() {
		boolean aurki;
		aurki = ize.getText().length() > 0 && tele.getText().length() > 0
				&& helb.getText().length() > 0;
		return aurki;
	}

	private void boxCarga() throws SQLException {
		List<String> lista = abok.getHiriak();
		hiri.removeAllItems();
		if (lista != null) {
			for (String hiria : lista) {
				hiri.addItem(hiria);
			}
		}
	}

	public void eremuakGarbitu() {
		hiri.setSelectedIndex(0);
		ize.setText("");
		helb.setText("");
		tele.setText("");
		ariZ.setText("");
	}
}